<?php
namespace Gotcha\Core {
    use Gotcha\Core\Components\DependencyFactory\Interfaces\IDependencyContainer;

    final class AppKernel {
        protected $_IDependencyContainer;

        public function __construct(
            IDependencyContainer $IDependencyContainer
        ) {
            $this->_IDependencyContainer = $IDependencyContainer;
        }

        public function initializeBootstrap() {
            $this->setApplicationConfig();
            $this->setApplicationLocalization();
        }

        private function setApplicationConfig() {
            $xmlReader = $this->_IDependencyContainer->returnDependency('XMLReader');
            $xmlReader->createXMLObject(CONFIG_DIR . 'AppConfig.xml', null, true);

            // Get DTO instance
            $appConfig = $this->_IDependencyContainer->returnDependency('DTOAppConfig');

            // Typecasting SimpleXML object makes things easier
            // to maintain so we prepare each node from AppConfig
            // to iterate through
            $appConfigArray = new \ArrayObject();
            $configObjectToArray = new \ArrayObject((array)$xmlReader->getXMLContent());
            $deploymentConfig = new \ArrayObject((array)$configObjectToArray->getArrayCopy()['deployment']);
            $securityConfig = new \ArrayObject((array)$configObjectToArray->getArrayCopy()['security']);
            $tokensConfig = new \ArrayObject((array)$securityConfig['tokens']);
            $formsConfig = new \ArrayObject((array)$securityConfig['forms']);
            $authenticationConfig = new \ArrayObject((array)$securityConfig['authentication']);
            $localizationConfig = new \ArrayObject((array)$configObjectToArray->getArrayCopy()['localization']);

            // To avoid adding foreach() loop each time new node is appended
            // to AppConfig, merge all arrays into one
            $appConfigMergedArrays = new \ArrayObject(array_merge(
                $deploymentConfig->getArrayCopy()['add'],
                $tokensConfig->getArrayCopy()['add'],
                $formsConfig->getArrayCopy()['add'],
                $authenticationConfig->getArrayCopy()['add'],
                $localizationConfig->getArrayCopy()['add'])
            );

            // Iterate through specific nodes
            foreach( $appConfigMergedArrays->getArrayCopy() as $row) {
                $xmlReader->getNodeKeyValue($row, $appConfigArray);
            }

            // Set application config DTO
            foreach($appConfigArray->getArrayCopy() as $key => $value) {
                $appConfig->$key = $value;
            }
        }

        private function setApplicationLocalization() {
            $cultureInfo = $this->_IDependencyContainer->returnDependency('CultureInfo');
            $cultureInfo->setDefaultLocale();
            $cultureInfo->setNumberFormatter();
            $cultureInfo->setCollator();
        }
    }
}